<?php
/*
 * Get spork! recipe stats
 */


// count total users
$result = DB::sql("SELECT count(*) as total_users FROM user");
$response = array();
foreach ($result as $row):
	$total_users = $row['total_users'];
endforeach;

// count total recipes
$result = DB::sql("SELECT count(*) as total_recipes FROM recipes");
$response = array();
foreach ($result as $row):
	$total_recipes = $row['total_recipes'];
endforeach;

// count recipes uploaded last month
$result = DB::sql("SELECT count(*) as recipes_last_month FROM recipes where date>(now()-30)");
$response = array();
foreach ($result as $row):
	$recipes_last_month = $row['recipes_last_month'];
endforeach;

// count recipes uploaded this month
$result = DB::sql("SELECT count(*) as recipes_this_month FROM recipes where month(date)=month(now())");
$response = array();
foreach ($result as $row):
	$recipes_this_month = $row['recipes_this_month'];
endforeach;

// count recipes tried today
$result = DB::sql("SELECT count(*) as tried_today FROM ratings where date(timestamp)=curdate()");
$response = array();
foreach ($result as $row):
	$tried_today = $row['tried_today'];
endforeach;

// count today's new recipes
$result = DB::sql("SELECT count(*) as recipes_today FROM recipes where date(date)=date(now())");
$response = array();
foreach ($result as $row):
	$recipes_today = $row['recipes_today'];
endforeach;

// count 5-star recipes
$result = DB::sql("select count(average) as five_star from (select avg(rating) as average from ratings group by rid) a where a.average=5");
$response = array();
foreach ($result as $row):
	$five_star = $row['five_star'];
endforeach;


header('HTTP/1.1 201');
header("Content-Type: application/json");
echo json_encode(array("total_users" => $total_users, "total_recipes" => $total_recipes, "recipes_last_month" => $recipes_last_month, "tried_today" => $tried_today, "recipes_today" => $recipes_today, "five_star" => $five_star, "recipes_this_month"=>$recipes_this_month));

?>
